package com.game.action;

import akka.actor.ActorRef;
import com.coment.message.roles.Resurrection;
import com.game.framework.ActorManager;
import com.game.framework.lock.LooksManager;
import com.game.framework.lock.annotation.ActionLock;
import com.game.logic.fighting.actor.RolesActor;
import com.game.logic.fighting.pve.FightingService;
import com.xs.action.annotate.ActionController;
import com.xs.action.annotate.ActionMethod;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.concurrent.locks.ReentrantLock;

/// 3-战斗
@RestController
@RequestMapping(value = "/fightAction")
@ActionController(value = 3, name = "战斗")
public class FightAction {
    @Resource
    private FightingService fightingService;


    /**
     * 1.范围攻击
     *
     * @param anchorInfoId 主播id
     * @param attacker     攻击者
     * @param target       被打的
     */
    @ActionLock(parameterIndex = {"#attacker"})
    @GetMapping("/areaAttack")
    @ActionMethod(value = 1, name = "范围攻击")
    public void areaAttack(
            @NotNull(message = "游戏主持主播") Integer anchorInfoId,
            @NotNull(message = "攻击者的哈希id不能为空") String attacker,
            @NotNull(message = "目标哈希id不能为空") String[] target
    ) {
        fightingService.attack(anchorInfoId, attacker, target);
    }

    /**
     * 4.复活
     *
     * @param anchorInfoId 主播id
     * @param rolesId      角色id
     */
    @ActionLock(parameterIndex = {"#rolesId"})
    @GetMapping("/resurrection")
    @ActionMethod(value = 4, name = "复活")
    public void resurrection(
            @NotNull(message = "游戏主持主播") Integer anchorInfoId,
            @NotNull(message = "角色哈希id不能为空") String rolesId
    ) {
        ActorManager.getActor(RolesActor.class, rolesId).tell(new Resurrection(), ActorRef.noSender());
    }

    /**
     * 5.技能释放
     *
     * @param anchorInfoId     主播id
     * @param rolesId          角色id
     * @param effectOnTheEnemy 技能对目标的效果
     * @param effectOnFriendly 技能对友方效果
     */
    @ActionLock(parameterIndex = {"#rolesId"})
    @GetMapping("/useSkill")
    @ActionMethod(value = 5, name = "技能释放")
    public void useSkill(
            @NotNull(message = "游戏主持主播") Integer anchorInfoId,
            @NotNull(message = "角色哈希id不能为空") String rolesId,
            @NotNull String[] effectOnTheEnemy,
            @NotNull String[] effectOnFriendly
    ) {
        fightingService.tryUseSkill(anchorInfoId, rolesId, effectOnTheEnemy, effectOnFriendly);
    }


    /**
     * 6.释放宠物技能
     *
     * @param anchorInfoId 主播id
     * @param userInfoId   用户id
     * @param rolesId      角色id
     * @param userPetId    宠物id
     * @param targetIds    技能释放的目标
     */
    @ActionLock(parameterIndex = {"#rolesId", "#userPetId"})
    @RequestMapping("/usePetSkill")
    @ActionMethod(value = 6, name = "释放宠物技能")
    public void usePetSkill(
            Integer anchorInfoId,
            Integer userInfoId,
            String rolesId,
            String userPetId,
            String[] targetIds
    ) {
        fightingService.usePetSkill(
                anchorInfoId,
                userInfoId,
                rolesId,
                userPetId,
                targetIds
        );
    }

    /// 7-怪物 3倍伤害攻击
    @ActionLock(parameterIndex = {"#attacker"})
    @GetMapping("/attack3")
    @ActionMethod(value = 7, name = "怪物 3倍伤害攻击")
    public void attack3(
            @NotNull(message = "游戏主持主播") Integer anchorInfoId,
            @NotNull(message = "攻击者的哈希id不能为空") String attacker,
            @NotNull(message = "目标哈希id不能为空") String[] target
    ) {

        ReentrantLock lock = LooksManager.roleActionLocks.computeIfAbsent(attacker, k -> new ReentrantLock());
        lock.lock();
        try {
            fightingService.attack3(anchorInfoId, attacker, target);
        } finally {
            lock.unlock();
        }

    }


    /// 8-杨戬触发护盾伤害
    @ActionLock(parameterIndex = {"#attacker"})
    @GetMapping("/skill10")
    @ActionMethod(value = 8, name = "杨戬触发护盾伤害")
    public void skill10(
            Integer anchorInfoId, String attacker, String[] target
    ) {
        fightingService.skill10(anchorInfoId, attacker, target);
    }
}

